$(function() {
	$.ajaxSetup ({
	    cache: false //关闭AJAX相应的缓存
	});

	
	if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style) {
		var dHeight = $(window).height();
		$(".gray_bg").css("height", dHeight);
	}
	
	//prohibit refresh by using F5 and enter
	/*document.onkeydown = function() {
		if (event.keyCode == 116 || event.keyCode == 13) {
			event.keyCode = 0;
			event.returnValue = false;
		}
	};
	*/
	//prohibit refresh by using F5 and enter 兼容firefox
	document.onkeydown = function(e) {
		var keynum;
		e = window.event || e;
		keynum = e.keyCode || e.which;
		if(keynum == 116 || keynum == 13){
			e.keyCode = 0;
			e.returnValue = false;
		}
	};
	
	//ajax load datamgr module
	$("#menu_datamgr").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("active");
		$.get("datamgr", function(data) {
			$(".right_td").html(data);
		});
	});

	//ajax load extension operation module
	$("#menu_ext").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("active");
		$.get("extension", function(data) {
			$(".right_td").html(data);
		});
	});

	//ajax load audit module
	$("#menu_audit").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("active");
		$.get("audit", function(data) {
			$(".right_td").html(data);
		});
	});

	//ajax load feedback module
	$("#menu_feedback").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("active");
		$.get("feedback", function(data) {
			$(".right_td").html(data);
		});
	});

	//ajax load usermgr module
	$("#menu_usermgr").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("active");
		$.get("usermgr", function(data) {
			$(".right_td").html(data);
		});
	});

	$("#usermgr_module").live("click", function() {
		removeAllActiveClassByClassName(".nor");
		$(this).parent().addClass("active");
		$.get("usermgr/module/usermgr", function(data) {
			$("#authmgr_module_container").html(data);
		});
	});

	$("#rolemgr_module").live("click", function() {
		removeAllActiveClassByClassName(".nor");
		$(this).parent().addClass("active");
		$.get("usermgr/module/rolemgr", function(data) {
			$("#authmgr_module_container").html(data);
		});
	});

	$("#authmgr_module").live("click", function() {
		removeAllActiveClassByClassName(".nor");
		$(this).parent().addClass("active");
		$.get("usermgr/module/authmgr", function(data) {
			$("#authmgr_module_container").html(data);
		});
	});

	$("#user_query_btn").live("click", function() {
		$.post("usermgr/module/usermgr/users", {
			"username" : $("#query_username").val()
		}, function(data) {
			$("#userquery_list").html(data);
		});
	});

	$("#menu_user_query").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("focus");
		$("#caidan1").addClass("active");
		$.get("data_mgr/user_query", function(data) {
			$(".right_td").html(data);
		});
	});

	$("#menu_message_query").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("focus");
		$("#caidan1").addClass("active");
		$.get("data_mgr/message_query", function(data) {
			$(".right_td").html(data);
		});
	});

	$("#menu_school_mgr").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("focus");
		$("#caidan1").addClass("active");
		$.get("data_mgr/school_query", function(data) {
			$(".right_td").html(data);
		});
	});

	$("#menu_book_import").live("click", function() {
		removeAllActive();
		removeAllFocusClass();
		$(this).parent().addClass("focus");
		$("#caidan1").addClass("active");
		$.get("data_mgr/import_book", function(data) {
			$(".right_td").html(data);
		});
	});
	
//	$("#menu_data_class_use_records").live("click", function() {
//		removeAllActive();
//		removeAllFocusClass();
//		$(this).parent().addClass("focus");
//		$("#caidan1").addClass("active");
//		$.post("data_mgr/class_use_records", function(data) {
//			$(".right_td").html(data);
//		});
//	});
//	
//	$("#menu_data_class_teachers_use_records").live("click", function() {
//		removeAllActive();
//		removeAllFocusClass();
//		$(this).parent().addClass("focus");
//		$("#caidan1").addClass("active");
//		$.post("data_mgr/data_class_teachers_use_records", function(data) {
//			$(".right_td").html(data);
//		});
//	});

	$("#menu_datamgr_depreate").live("click",function(){
		$("li.caidan").slideToggle();
	});
	
	$("a.btn_new_school").live("click", function(data) {
		$.get("data_mgr/school_query/schools?form", function(data) {
			$(".right_td").html(data);
		});
	});

	//学校管理tab切换之老师管理
	$("#school_mgr_tab_menu_teacher").live("click", function() {
		var sid = $(this).attr("sid");
		removeAllOnClassInSchoolMgrTab();
		$(this).addClass("on");
		$(".gray_bg").show();
		$.get("data_mgr/school_mgr/teacher_mgr_module", {
			"sid" : sid
		}, function(data) {
			$("#tab_switch_area").html(data);
			$(".gray_bg").hide();
		});
	});
	//学校管理tab切换之班级管理
	$("#school_mgr_tab_menu_class").live("click", function() {
		var sid = $(this).attr("sid");
		removeAllOnClassInSchoolMgrTab();
		$(this).addClass("on");
		$(".gray_bg").show();
		$.get("data_mgr/school_mgr/class_mgr_module", {
			"sid" : sid
		}, function(data) {
			$("#tab_switch_area").html(data);
			$(".gray_bg").hide();
		});
	});
	//学校管理tab切换之学生管理
	$("#school_mgr_tab_menu_student").live("click", function() {
		var sid = $(this).attr("sid");
		removeAllOnClassInSchoolMgrTab();
		$(this).addClass("on");
		$(".gray_bg").show();
		$.get("data_mgr/school_mgr/student_mgr_module", {
			"sid" : sid
		}, function(data) {
			$("#tab_switch_area").html(data);
			$(".gray_bg").hide();
		});
	});
	//学校管理tab切换之学校信息管理
	$("#school_mgr_tab_menu_school").live("click", function() {
		var sid = $(this).attr("sid");
		removeAllOnClassInSchoolMgrTab();
		$(this).addClass("on");
		$(".gray_bg").show();
		$.get("data_mgr/school_mgr/school_info_module/" + sid, function(data) {
			$("#tab_switch_area").html(data);
			$(".gray_bg").hide();
		});
	});
});

function removeAllActiveClassByClassName(className) {
	$(className).each(function() {
		$(this).removeClass("active");
	});
}

//ajax load remove all menu's active class
function removeAllActive() {
	$("li.menuStyle").each(function() {
		$(this).removeClass("active");
	});
}
function removeAllOnClassInSchoolMgrTab() {
	$("a.school_mgr_tab_a").each(function() {
		$(this).removeClass("on");
	});
}

function removeAllFocusClass() {
	$("li.caidan").find("p").each(function() {
		$(this).removeClass("focus");
	});
}

function logout() {
	pretty_confirm("确定要退出吗?",logoutTrue);
}

function logoutTrue(){
	$.get("logout", function(data) {
		window.location.href = "";
	});
}

function schooldetails(sid) {
	$.get("data_mgr/school_query/schools/" + sid, function(data) {
		$(".right_td").html(data);
	});
}
//检查文件
function extractFileName(string) {
	if (string.indexOf('/') > -1) {
		fileName = string.substring(string.lastIndexOf('/') + 1, string.length);
	} else {
		fileName = string
				.substring(string.lastIndexOf('\\') + 1, string.length);
	}
	return fileName;
}

function shutDown() {
	$("#error_tx").hide();
	$("#error_tx").remove();
	$(".gray_bg").hide();
}

function return2SchoolQuery() {
	$.get("data_mgr/school_query", function(data) {
		$(".right_td").html(data);
	});
}

function clickSchoolInfoMenu4Refresh(){
	var sid = $("#school_mgr_tab_menu_school").attr("sid");
	$.get("data_mgr/school_mgr/school_info_module/" + sid, function(data) {
		$("#tab_switch_area").html(data);
	});
}

function return2TeacherQueryState() {
	var sid = $("#school_mgr_tab_menu_teacher").attr("sid");
	$(".gray_bg").show();
	$.get("data_mgr/school_mgr/teacher_mgr_module", {
		"sid" : sid
	}, function(data) {
		$("#tab_switch_area").html(data);
		$(".gray_bg").hide();
	});
	//qTeachers();
}

function return2StudentQueryState() {
	var sid = $("#school_mgr_tab_menu_teacher").attr("sid");
	$(".gray_bg").show();
	$.get("data_mgr/school_mgr/student_mgr_module", {
		"sid" : sid
	}, function(data) {
		$("#tab_switch_area").html(data);
		$(".gray_bg").hide();
	});
}

function slideLeft() {
	$("#slide_td").toggle("fast");
}

/**
 * 	confirm插件。   样式是初始化的时候给的。 
 * 	消息提示是调用的时候参数传进去的。
 * 	返回值固定。 888说明是确定按钮  
 */
var confirmTargetDom;

function pretty_notice(message){
	unBlock();
	var _$_confirm = "";
	_$_confirm += "<div class='add_pop_box' id='prettyconfirmpop'>";
	_$_confirm += 	"<h1><span>信息提示</span></h1>";
	_$_confirm +=   	"<div class='in'>";
	_$_confirm +=			"<table width='0%' border='0' cellspacing='0' cellpadding='0'>";
	_$_confirm +=				"<tr>";
	_$_confirm +=					"<td align='left' valign='top'>";
	_$_confirm +=						"<div class='drxx_rs'>";
	_$_confirm +=						"<ul>";
	_$_confirm +=							"<li>"+message+"</li>";
	_$_confirm +=						"</ul>";
	_$_confirm +=					"</td>";
	_$_confirm +=				"</tr>";
	_$_confirm +=				"<tr>";
	_$_confirm +=					"<td height='60' align='center'>";
	_$_confirm +=						"<input id='prettyconfirmsurebtn' class='queding' type='button' value='确定'/>";
	_$_confirm +=					"</td>";
	_$_confirm +=				"</tr>";
	_$_confirm +=			"</table>";
	_$_confirm +=		"</div>";
	_$_confirm += "</div>";
	$(_$_confirm).appendTo("body");
	/*  以后把layout 中的<div class='gray_bg'></div>去掉。在这里动态插入和移除
	var _$_mask = "";
	_$_mask += 	"<div class='gray_bg'></div>";
	$(_$_mask).appendTo("body");
	*/
	$(".gray_bg").show();
	
	$("#prettyconfirmsurebtn").die("click").live("click",unBlock);
}

function pretty_confirm(message,callback){
	//var targetDomCls = "_unique_chin_confirm_cls";
	unBlock();
	var _$_confirm = "";
	_$_confirm += "<div class='add_pop_box' id='prettyconfirmpop'>";
	_$_confirm += 	"<h1><span>信息提示</span></h1>";
	_$_confirm +=   	"<div class='in'>";
	_$_confirm +=			"<table width='0%' border='0' cellspacing='0' cellpadding='0'>";
	_$_confirm +=				"<tr>";
	_$_confirm +=					"<td align='left' valign='top'>";
	_$_confirm +=						"<div class='drxx_rs'>";
	_$_confirm +=						"<ul>";
	_$_confirm +=							"<li>"+message+"</li>";
	_$_confirm +=						"</ul>";
	_$_confirm +=					"</td>";
	_$_confirm +=				"</tr>";
	_$_confirm +=				"<tr>";
	_$_confirm +=					"<td height='60' align='center'>";
	_$_confirm +=						"<input id='prettyconfirmsurebtn' class='queding' type='button' value='确定'/>";
	_$_confirm +=						"<input id='prettyconfirmcancelbtn' class='quxiao' type='button' value='取消'/>";
	_$_confirm +=					"</td>";
	_$_confirm +=				"</tr>";
	_$_confirm +=			"</table>";
	_$_confirm +=		"</div>";
	_$_confirm += "</div>";
	$(_$_confirm).appendTo("body");
	/*  以后把layout 中的<div class='gray_bg'></div>去掉。在这里动态插入和移除
	var _$_mask = "";
	_$_mask += 	"<div class='gray_bg'></div>";
	$(_$_mask).appendTo("body");
	*/
	$(".gray_bg").show();
	$("#prettyconfirmcancelbtn").die("click").live("click",function(){
		$("body").find("#prettyconfirmpop").remove();
		$(".gray_bg").hide();
		//$("body").find(".gray_bg").remove();
	});
	
	$("#prettyconfirmsurebtn").die("click").live("click",callback);
}

function pretty_confirm_with_cancel_call(message,callback,cancelCallback){
	//var targetDomCls = "_unique_chin_confirm_cls";
	unBlock();
	var _$_confirm = "";
	_$_confirm += "<div class='add_pop_box' id='prettyconfirmpop'>";
	_$_confirm += 	"<h1><span>信息提示</span></h1>";
	_$_confirm +=   	"<div class='in'>";
	_$_confirm +=			"<table width='0%' border='0' cellspacing='0' cellpadding='0'>";
	_$_confirm +=				"<tr>";
	_$_confirm +=					"<td align='left' valign='top'>";
	_$_confirm +=						"<div class='drxx_rs'>";
	_$_confirm +=						"<ul>";
	_$_confirm +=							"<li>"+message+"</li>";
	_$_confirm +=						"</ul>";
	_$_confirm +=					"</td>";
	_$_confirm +=				"</tr>";
	_$_confirm +=				"<tr>";
	_$_confirm +=					"<td height='60' align='center'>";
	_$_confirm +=						"<input id='prettyconfirmsurebtn' class='queding' type='button' value='确定'/>";
	_$_confirm +=						"<input id='prettyconfirmcancelbtn' class='quxiao' type='button' value='取消'/>";
	_$_confirm +=					"</td>";
	_$_confirm +=				"</tr>";
	_$_confirm +=			"</table>";
	_$_confirm +=		"</div>";
	_$_confirm += "</div>";
	$(_$_confirm).appendTo("body");
	/*  以后把layout 中的<div class='gray_bg'></div>去掉。在这里动态插入和移除
	var _$_mask = "";
	_$_mask += 	"<div class='gray_bg'></div>";
	$(_$_mask).appendTo("body");
	*/
	$(".gray_bg").show();
	$("#prettyconfirmcancelbtn").die("click").live("click",function(){
		unBlock();
		cancelCallback();
		//$("body").find(".gray_bg").remove();
	});
	
	$("#prettyconfirmsurebtn").die("click").live("click",callback);
}

function unBlock(){
	$("body").find(".add_pop_box").remove();
	$(".gray_bg").hide();
	//$("#shouldmask").show();
	//$("body").find(".gray_bg").remove();
	removeGloablBindUnBlockEvent();
}
function unBlockErrmsg(){
	//$(".gray_bg").hide();
	$("body").find(".pop_err").remove();
	removeGloablBindUnBlockEvent();
}

function pretty_errmsg_notice(message){
	unBlockErrmsg();
	var _$_pretty_errmsg_notice = "";
	_$_pretty_errmsg_notice += "<div class='add_pop_box pop_err' id='prettynoticepop'>";
	_$_pretty_errmsg_notice += 		"<h1><span>信息提示</span></h1>";
	_$_pretty_errmsg_notice +=  	"<div class='in'>";
	_$_pretty_errmsg_notice +=			"<table width='0%' border='0' cellspacing='0' cellpadding='0'>";
	_$_pretty_errmsg_notice +=				"<tr>";
	_$_pretty_errmsg_notice +=					"<td align='left' valign='top'>";
	_$_pretty_errmsg_notice +=						"<ul>";
	_$_pretty_errmsg_notice +=							"<li class='err_msg'>错误提示："+message+"</li>";
	_$_pretty_errmsg_notice +=						"</ul>";
	_$_pretty_errmsg_notice +=					"</td>";
	_$_pretty_errmsg_notice +=				"</tr>";
	_$_pretty_errmsg_notice +=			"</table>";
	_$_pretty_errmsg_notice +=		"</div>";
	_$_pretty_errmsg_notice += "</div>";
	$(_$_pretty_errmsg_notice).appendTo("body");
	
	/*var _$_mask = "";
	_$_mask += 	"<div class='gray_bg'></div>";
	$(_$_mask).appendTo("body");
	*/
	$(".gray_bg").show();
	globalBindUnBlockEvent();
}

function globalBindUnBlockEvent(){
	$(document).bind("keydown keypress mousedown mouseup",function(){
		unBlockErrmsg();
	});
}

function removeGloablBindUnBlockEvent(){
	$(document).unbind("keydown keypress mousedown mouseup");
}